package com.fzj.example.thread.callablefuture;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * ClassName: CallableTest
 * date: 2019/11/5 15:14
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //Future&Callable
        ExecutorService executors=Executors.newCachedThreadPool();
        Map<String,Object> map=new HashMap(10);
        Task task=new Task(map);
        //内部还是借助了FutureTask
        Future<Map> future=executors.submit(task);
        System.out.println(future.get().get("test"));
        System.out.println("dfafa");
        //FutureTask
        FutureTask<Map> futureTask=new FutureTask<>(task);
        executors.submit(futureTask);
        if(futureTask.get()!=null){
            System.out.println(futureTask.get().get("test"));
        }

    }

    static class Task implements Callable{

        private Map<String,Object> map;

        public  Task(Map<String,Object> map){
            this.map=map;
        }

        @Override
        public Map<String,Object> call() throws Exception {
            map.put("test","test");
            return map;
        }
    }
}
